package com.copro.chemdb.result;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ihelpuoo.lang.StringUtils;

import com.copro.chemdb.ques.QuesUtils;
import com.copro.chemdb.ques.Questionnaire;
import com.copro.simple.Application;

public class EditResultAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Application app = Application.me(request);
		Result result = (Result) form;
		if (result.getId() > 0)
			result = ResultUtils.getResult(app, result.getId());
		result.setType(Result.FROM_QUEST);
		Questionnaire ques = QuesUtils.getById(app, result.getQuesId());

		List<String> segs = ResultUtils.renderSubjects(app, result.getQuesId(),
				result, "edit");
		List<Questionnaire> quesSet = QuesUtils.getChildren(app, result
				.getQuesId());
		request.setAttribute("ques", ques);
		request.setAttribute("segList", segs);
		request.setAttribute("result", result);
		request.setAttribute("quesSet", quesSet);

		if (StringUtils.equalsIgnoreCase("true", request
				.getParameter("forbidsub")))
			request.setAttribute("forbidsub", "true");

		return mapping.findForward("edit");
	}

}
